﻿using System;
using UnityEngine;

namespace UFrame
{
    public class ResolutionAdaptive : MonoBehaviour
    {
        public Camera camera;
        private float cameraAdjust;
        public float width;
        public float height;

        public void Start()
        {
            MainCameraAdjust2();
        }

        public void MainCameraAdjust()
        {
            if (camera == null)
                camera = Camera.main;

            cameraAdjust = camera.fieldOfView;
            float a1 = (width / height);
            float a2 = ((float) Screen.width / (float) Screen.height);
            float ratio = a1 / a2;
            if (camera.orthographic)
            {
                camera.orthographicSize = Mathf.Max(cameraAdjust, ratio * cameraAdjust);
            }
            else
            {
                camera.fieldOfView = Mathf.Max(cameraAdjust, ratio * cameraAdjust);
            }

            Debug.Log(ratio);
        }


        public void MainCameraAdjust2()
        {
            if (Screen.width == 1800 && Screen.height == 2400)
            {
                camera.fieldOfView = 97;
                return;
            }

            float manualHeight = 0;
            float a1 = Convert.ToSingle(Screen.height) / Screen.width;
            float a2 = Convert.ToSingle(height) / width;

            if (a1 > a2)
            {
                manualHeight = (width / Screen.width) * Screen.height;
            }
            else
            {
                manualHeight = height;
            }

            float scale = manualHeight / height;

//        Debug.Log("计算的比列: " + scale);
            if (camera.orthographic)
            {
                camera.orthographicSize *= scale;
            }
            else
            {
                if (scale > 1)
                {
                    scale -= scale * 0.1f;
                }


                camera.fieldOfView *= scale;
            }
        }
    }
}